package edu.nf.exam.controller;

import cn.hutool.core.convert.Convert;
import edu.nf.exam.entity.ErrorQuestionInfo;
import edu.nf.exam.entity.PaperQuestionInfo;
import edu.nf.exam.service.ErrorQuestionService;
import edu.nf.exam.service.impl.ErrorQuestionServiceImpl;
import edu.nf.exam.service.impl.PaperQuestionServiceImpl;
import org.nf.mvc.annotation.RequestMapping;
import org.nf.mvc.servlet.View;
import org.nf.mvc.view.JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : LLH
 * @create 2021/7/8
 */
public class ErrorQuestionController extends BaseController{

    /**
     * 添加
     * @param
     * @return
     */
    @RequestMapping("/errorQuestion/add")
    public View add(HttpServletRequest request){
        //试卷结果ID
        Integer examPaperResultId = Integer.valueOf(request.getParameter("examPaperResultId"));
        //用户ID
        Integer createUser = Integer.valueOf(request.getParameter("createUser"));
        //学科
        String subjectName = request.getParameter("subjectName");
        //年级
        String levelName = request.getParameter("levelName");
        //试卷名称
        String examPaperName = request.getParameter("examPaperName");
        //考生做题答案
        String[] stuAnswers = request.getParameterValues("stuAnswer");
        //正确答案
        String[] realAnswers = request.getParameterValues("realAnswer");
        //试题ID
        Integer[] paperQuestionIds = Convert.toIntArray(request.getParameterValues("paperQuestionIds"));
        //试题题型
        Integer[] questionTypes = Convert.toIntArray(request.getParameterValues("questionTypes"));
        //试题分数
        Integer[] scores = Convert.toIntArray(request.getParameterValues("scores"));

        ErrorQuestionInfo errorQuestionInfo=new ErrorQuestionInfo();
        errorQuestionInfo.setExamPaperResultId(examPaperResultId);
        errorQuestionInfo.setCreateUser(createUser);
        errorQuestionInfo.setLevelName(levelName);
        errorQuestionInfo.setSubjectName(subjectName);
        errorQuestionInfo.setExamPaperName(examPaperName);

        ErrorQuestionService service=new ErrorQuestionServiceImpl();

        int num=0;
        // 循环遍历
        for(int i=0;i<paperQuestionIds.length;i++){
            errorQuestionInfo.setPaperQuestionId(paperQuestionIds[i]);
            errorQuestionInfo.setQuestionType(questionTypes[i]);
            errorQuestionInfo.setAnswer(stuAnswers[i]);

            // 判断学生的答案与正确答案是否匹配
            if(realAnswers[i].equals(stuAnswers[i])){
                errorQuestionInfo.setCustomerScore(scores[i]);
                errorQuestionInfo.setDoRight(1);
            }else{
                errorQuestionInfo.setCustomerScore(0);
                errorQuestionInfo.setDoRight(2);
            }

            int r = service.add(errorQuestionInfo);
            num=num+r;
        }

        // 验证service成功执行数
        if(num==paperQuestionIds.length){
            return new JsonView(success());
        }else{
            return new JsonView(fail(500,"添加失败"));
        }

    }

    /**
     * 查询
     * @param errorQuestionInfo
     * @return
     */
    @RequestMapping("/errorQuestion/select")
    public View select(ErrorQuestionInfo errorQuestionInfo){
        ErrorQuestionService service=new ErrorQuestionServiceImpl();
        try {
            List<ErrorQuestionInfo> list = service.select(errorQuestionInfo);
            return new JsonView(success(list));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }


    /**
     * 根据答卷结果ID查询其所有答题数据及各题型数量
     * @param errorQuestionInfo
     * @return
     */
    @RequestMapping("/errorQuestion/selectByExamPaperResultId")
    public View selectByExamPaperResultId(ErrorQuestionInfo errorQuestionInfo){
        ErrorQuestionService service=new ErrorQuestionServiceImpl();
        // 用于统计单选题数量
        int singleCount=0;
        // 用于统计多选题数量
        int multipleCount=0;
        // 用于统计判断题数量
        int judgeCount=0;

        try {
            List<ErrorQuestionInfo> list = service.select(errorQuestionInfo);

            // 循环遍历
            for (ErrorQuestionInfo e : list) {
                // 判断其题型
                if(e.getQuestionType()==1){
                    singleCount++;
                }else if(e.getQuestionType()==2){
                    multipleCount++;
                }else if(e.getQuestionType()==3){
                    judgeCount++;
                }
            }
            JsonView jsonView = new JsonView(success(list,singleCount,multipleCount,judgeCount));
            jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
            return jsonView;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 根据答卷ID统计各题型错题数量
     * @param errorQuestionInfo
     * @return
     */
    @RequestMapping("/errorQuestion/selectErrorByExamPaperResultId")
    public View selectErrorByExamPaperResultId(ErrorQuestionInfo errorQuestionInfo){
        ErrorQuestionService service=new ErrorQuestionServiceImpl();
        // 用于统计单选题数量
        int singleCount=0;
        // 用于统计多选题数量
        int multipleCount=0;
        // 用于统计判断题数量
        int judgeCount=0;

        try {
            List<ErrorQuestionInfo> list = service.select(errorQuestionInfo);

            // 循环遍历
            for (ErrorQuestionInfo e : list) {
                // 判断其题型
                if(e.getQuestionType()==1 && e.getDoRight()==2){
                    singleCount++;
                }else if(e.getQuestionType()==2 && e.getDoRight()==2){
                    multipleCount++;
                }else if(e.getQuestionType()==3 && e.getDoRight()==2){
                    judgeCount++;
                }
            }
            JsonView jsonView = new JsonView(success(list,singleCount,multipleCount,judgeCount));
            jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
            return jsonView;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }
}
